package com.rockgle.reader;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageButton;
import android.widget.ScrollView;
import android.widget.TextView;

/**
 * Representa la actividad que muestra una entrada de Rockgle
 * @author Javi
 *
 */
public class EntryViewer extends Activity{
	private SortedEntryList data;
	private TextView mTitle;
	private TextView mFecha;
	private WebView mContent;
	private ImageButton buttonUp;
	private ImageButton buttonDown;
	private ViewGroup banner;
	private ScrollView scroll;
	private int currentEntryShowed;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.entryviewer);
		
		this.mTitle = (TextView)findViewById(R.id.viewertitleview);
		this.mFecha = (TextView)findViewById(R.id.viewerupdatedview);
		this.mContent = (WebView)findViewById(R.id.viewerwebview);
		this.buttonUp = (ImageButton)findViewById(R.id.ButtonUP);
		this.buttonDown = (ImageButton)findViewById(R.id.ButtonDOWN);
		this.banner = (ViewGroup)findViewById(R.id.viewerheader);
		this.scroll = (ScrollView)findViewById(R.id.viewerscroller);
		
		EntryViewOnClickListener onclick = new EntryViewOnClickListener(this);
		mContent.getSettings().setJavaScriptEnabled(true);
		mContent.getSettings().setPluginState(WebSettings.PluginState.ON_DEMAND);
		
		this.buttonUp.setOnClickListener(onclick);
		this.buttonDown.setOnClickListener(onclick);
	    
	    Bundle b = getIntent().getExtras(); 
	    currentEntryShowed = b.getInt("Position");
	    data=((CustomApplication)getApplication()).getData();
	    if(getResources().getConfiguration().orientation==Configuration.ORIENTATION_LANDSCAPE){
			banner.setVisibility(View.GONE);
		}
	    showEntry(currentEntryShowed);
	}
	
	public boolean isShowingLast(){
		return currentEntryShowed == (data.size()-1);
	}
	
	
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onConfigurationChanged(android.content.res.Configuration)
	 */
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		if (newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){
			banner.setVisibility(View.GONE);
			
		}
		if (newConfig.orientation==Configuration.ORIENTATION_PORTRAIT){
			banner.setVisibility(View.VISIBLE);
		}
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		mContent.clearView();
		mContent.destroy();
		this.finish();
	}

	/**
	 * @return the currentEntryShowed
	 */
	public int getCurrentEntryShowed() {
		return currentEntryShowed;
	}

	public void showEntry(int index){
		this.currentEntryShowed = index;
		Entry entry = data.get(index);
		
		mContent.loadDataWithBaseURL("http://www.rockgle.com", entry.getContent(), "text/html", "utf-8", null);
		mTitle.setText(entry.getTitle());
		
		StringBuilder text = new StringBuilder(entry.getPublishedPrettyPrint());
		text.append(" por ");
		text.append(entry.getAuthor());
        mFecha.setText(text.toString());
        
        scroll.fullScroll(View.FOCUS_UP);
        entry.markAsRead();
	}
}
